package 力扣.贪心;

import java.util.Arrays;

public class 做菜顺序1402 {
    public static void main(String[] args) {
//        int[] ints = {-2,5,-1,0,3,-3};
//        int[] ints = {4,3,2};
//        int[] ints = {-1,-4,-5};
        int[] ints = {1};
        int ints1 = maxSatisfaction(ints);
        System.out.println(ints1);
    }
    public static int maxSatisfaction(int[] satisfaction) {
        int n=satisfaction.length;
        int maxSatisfaction = 0;
        Arrays.sort(satisfaction);//原地排序
        if (satisfaction[n-1]<=0){
            return 0;
        }
        //一步一步的贪心
        for (int i = 0; i <n ; i++) {
               int flag=1;
               int j=i;
               int thisMax=maxSatisfaction;
               maxSatisfaction=0;
               //核心代码
               while (i<n){
                   maxSatisfaction=(satisfaction[i]*flag)+maxSatisfaction;
                   flag++;
                   i++;
               }
               if (thisMax>maxSatisfaction){
                   maxSatisfaction=thisMax;
               }
                if (satisfaction[j]>0){
                    return maxSatisfaction;
                }
               i=j;
        }
        return maxSatisfaction;

    }
}
